package com.bite.system.manager;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bit.common.redis.service.RedisService;
import com.bite.common.core.constants.CacheConstants;
import com.bite.common.core.domain.BaseEntity;
import com.bite.system.domain.exam.UserExam;
import com.bite.system.domain.user.User;
import com.bite.system.mapper.user.UserExamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public class UserCacheManager {

    @Autowired
    private RedisService redisService;

    @Autowired
    private UserExamMapper userExamMapper;

    public void refreshUser(User user) {
        String userKey = getUserKey(user.getUserId());
        if (redisService.getCacheObject(userKey, User.class) == null) {
            return;
        }
        redisService.setCacheObject(userKey, user, CacheConstants.USER_EXP, TimeUnit.MINUTES);
    }

    // 添加已经报名的用户
    public void addUserExamWithEntered(Long examId) {
        Set<Long> userIdList = userExamMapper.selectUserIdByExamId(examId);
        // TODO 可以批量更新吗？
        for (Long userId : userIdList) {
            redisService.leftPushForList(getUserExamKey(userId), examId);
        }
    }

    public void delUserExamWithEntered(Long examId) {
        Set<Long> userIdList = userExamMapper.selectUserIdByExamId(examId);
        for (Long userId : userIdList) {
            redisService.removeForList(getUserExamKey(userId), examId);
        }
    }

    public void refreshUserExam(List<Long> userIdList) {
        refreshUserExam(new HashSet<>(userIdList));
    }

    public void refreshUserExam(Set<Long> userIdList) {
        if (CollectionUtil.isEmpty(userIdList)) return;

        List<String> userExamKeyList = userIdList.stream().map(this::getUserExamKey).toList();
        redisService.deleteMultiObject(userExamKeyList);

        List<UserExam> allUserExamList = userExamMapper.selectList(new LambdaQueryWrapper<UserExam>()
                        .select(UserExam::getExamId, UserExam::getUserId)
                        .in(UserExam::getUserId, userIdList)
                        .orderByDesc(BaseEntity::getCreateTime));
        Map<Long, List<UserExam>> userExamMap = allUserExamList.stream().collect(Collectors.groupingBy(UserExam::getUserId));
        userExamMap.forEach((userId, userExamList) -> {
            List<Long> userExamIdList = userExamList.stream().map(UserExam::getExamId).toList();
            // TODO 是否可以批量更新
            redisService.rightPushAll(getUserExamKey(userId), userExamIdList);
        });
    }



    private String getUserKey(Long userId) {
        return CacheConstants.USER_DETAIL + userId;
    }

    private String getUserExamKey(Long userId) {
        return CacheConstants.USER_EXAM_LIST + userId;
    }


}
